Page({
  data: {
    empytList: [
      [{select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}],
      [{select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}],
      [{select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}],
      [{select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}],
      [{select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}],
      [{select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}],
      [{select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}, {select: false}]
    ],
    weekList: ['一', '二', '三', '四', '五', '六', '日'], // 星期几
    pitchList: [1, 2, 3, 4, 5, 6, 7], // 节数
    classRoom: [
      { room: '213', select: true },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false },
      { room: '213', select: false }
    ],
    // scheduleList: [
    //   {
    //     courseName: '体育'
    //   }
    // ]
    bIsPopupShow: false,
    weekIndex: 0,
    weekArray: ['第一周', '第二周', '第三周', '第四周', '第五周', '第六周', '第七周', '第八周', '第九周', '第十周'],
    buildingArray: [['A栋教学楼', 'B栋教学楼'], ['一层', '二层', '三层', '四层', '五层']],
    buildingIndex: [0, 1],
    dataList: [
      {
        name: '马克思理论',
        lesson: '02-02',
        dayTime: '2',
        borrowStatus: '0'
      },
      {
        name: '计算机基础和理论',
        lesson: '04-04',
        dayTime: '5',
        borrowStatus: '0'
      },
      {
        name: '体育',
        lesson: '07-07',
        dayTime: '4',
        borrowStatus: '0'
      },
      {
        name: '',
        lesson: '01-01',
        dayTime: '1',
        borrowStatus: '1'
      },
      {
        name: '',
        lesson: '01-01',
        dayTime: '4',
        borrowStatus: '1'
      }
    ], // 课程数据
    scheduleList: []
  },
  onLoad () {
    this.scheduleList()
  },
  goRecordList() {
    wx.navigateTo({
      url: '/pages/workService/recordList/index'
    })
  },
  scheduleList () {
    const { dataList } = this.data
    dataList.forEach((item, index) => {
      if (item.name.length > 5) {
        item.name = item.name.substring(0, 5) + '…'
      }
      item.lessonBegin = parseInt(item.lesson.split('-')[0]) // 第几节课开始
      item.lessonEnd = parseInt(item.lesson.split('-')[1]) // 第几节课结束
      item.lessonSum = (item.lessonEnd - item.lessonBegin) + 1 // 共有几节课
      // 每节课程表的样式
      const style = {
        left: ((item.dayTime - 1) * 98),
        top: (item.lessonBegin - 1) * 35,
        height: item.lessonSum * 35
      }
      switch (item.borrowStatus) {
        case '0':
          item.classStyle = `
            left: ${style.left}rpx;
            top: ${style.top}px;
            height: ${style.height}px;
            background-color: #2172ED;
          `
          break
        case '1':
          item.name = '已借用'
          item.classStyle = `
            left: ${style.left}rpx;
            top: ${style.top}px;
            height: ${style.height}px;
            background-color: #FFBE4D;
          `
          break
        default:
          break
      }
    })
    this.setData({
      scheduleList: dataList
    })
  },
  selectClassRoom (e) {
    const index = e.currentTarget.dataset.index
    const classRoom = [...this.data.classRoom]
    classRoom.forEach((item) => {
      item.select = false
    })
    classRoom[index].select = true
    this.setData({
      classRoom
    })
  },
  /**
   * @see 点击教室借用弹窗"取消"
   */
  handCancel () {
    this.setData({
      bIsPopupShow: false
    })
  },
  /**
   * @see 点击教室借用弹窗"提交"
   */
  handleConfirm () {
    this.setData({
      bIsPopupShow: false
    })
  },
  /**
   * @see 点击第X周
   */
  bindWeekChange (e) {
    this.setData({
      weekIndex: e.detail.value
    })
  },
  /**
   * @see 点击X栋教学楼X层
   */
  bindBuildingChange (e) {
    this.setData({
      buildingIndex: e.detail.value
    })
  },
  bindBuildingColumnChange (e) {
    var data = {
      buildingArray: this.data.buildingArray,
      buildingIndex: this.data.buildingIndex
    }
    data.buildingIndex[e.detail.column] = e.detail.value
    switch (e.detail.column) {
      case 0:
        switch (data.buildingIndex[0]) {
          case 0:
            data.buildingArray[1] = ['一层', '二层', '三层', '四层', '五层']
            break
          case 1:
            data.buildingArray[1] = ['一层', '二层', '三层', '四层', '五层']
            break
        }
        data.buildingIndex[1] = 0
        break
    }
    console.log(data.buildingIndex)
    this.setData(data)
  },
  /**
   * @see 点击课程表空的课程
   */
  bindEachClass (e) {
    const { week, lesson } = e.currentTarget.dataset
    const attr = `empytList[${week}][${lesson}].select`
    this.setData({
      [attr]: !this.data.empytList[week][lesson].select
    })
    console.log('点击课程表空的课程', week, lesson, attr)
  },
  /**
   * @see 点击“教室借用”
   */
  bindClassRoomBorrow (e) {
    this.setData({
      bIsPopupShow: true
    })
  }
})